package cn.itcast.babasport.utils.fast;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.io.FilenameUtils;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;

public class FastDFSUtils {

	public static String UploadPicToFastDFS(byte[] file_buff,String filename) throws Exception{
		//1、加载fastDFS的配置文件路径
		ClassPathResource resource = new ClassPathResource("fdfs_client.conf");
		//2、初始化fastDFS配置文件
		ClientGlobal.init(resource.getClassLoader().getResource("fdfs_client.conf").getPath());
		//3、创建跟踪服务器的客户端
		TrackerClient trackerClient = new TrackerClient();
		//4、根据跟踪服务器的额客户端获取服务端
		TrackerServer trackerServer = trackerClient.getConnection();
		//5、根据跟踪服务器获取到存储服务器的客户端
		StorageClient1 storageClient1 = new StorageClient1(trackerServer, null);
		//6、附件上传，返回附件的路径
		String file_ext_name = FilenameUtils.getExtension(filename); // 附件的扩展名
		//parm例如
		String path = storageClient1.upload_file1(file_buff, file_ext_name, null);
		return path;
	}

}
